package com.rrd.hive.udf;

import com.rrd.hive.util.DateUtils;
import com.rrd.hive.util.PIDValidator;
import com.weicai.caesar.CaesarUtil;
import org.apache.commons.collections.bag.SynchronizedSortedBag;
import org.apache.hadoop.hive.ql.exec.Description;
import org.apache.hadoop.hive.ql.exec.UDF;
import org.apache.hadoop.io.Text;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;


@Description(
        name = "loan_24month_status",
        value = "_FUNC_(List<String> list) - 计算进件24月还款状态"
)
public class AJXTLoan24MonthsStateUDF extends UDF {
    private static final SimpleDateFormat ymd_format = new SimpleDateFormat(
            "yyyy-MM-dd");
    public Text evaluate(String emit_date,List<String> list,String observe_date,String ajxt_get_date) {
        if (list == null || list.size()<=0) {
            return null;
        }
        StringBuilder sb = new StringBuilder();
        Collections.sort(list);
        if(!emit_date.equals(list.get(0).split(",")[0])){
            sb.append("*");
        }
        String arr[];
        String due_date="";
        String due_date_k="";
        int od_days=0;
        String pre_payoff_time="";
        int pre_periods=0;
        for(int i=0;i<list.size();i++){
            arr =list.get(i).split(",");
            if(arr[0].compareTo(observe_date)>0){
                break;
            }
            od_days=0;
            due_date=arr[0];
            due_date_k=com.rrd.hive.util.DateUtils.add_days(due_date,3);
            od_days=check_overdue_days(due_date_k,list,i,ajxt_get_date);
            sb.append(getState(od_days));
        }
       //如果借款到期后，仍然逾期，则每个月月末进行上报
        while(due_date.substring(0,7).compareTo(observe_date.substring(0,7))<0){
            due_date=next_month_lasyday(due_date);
            od_days=check_overdue_days(due_date,list,list.size()-1,ajxt_get_date);
            sb.append(getState(od_days));
        }
        Text res = new Text(format_loan_24month_status(sb.toString()));

        return res;
    }
   private static String getState(int od_days){
      String s="";
      if(od_days>=1 && od_days<=30){
          s="1";
      }else if(od_days>=31 && od_days<=60){
          s="2";
      }else if(od_days>=61 && od_days<=90){
          s="3";
      }else if(od_days>=91 && od_days<=120){
          s="4";
      }else if(od_days>=121 && od_days<=150){
          s="5";
      }else if(od_days>=151 && od_days<=180){
          s="6";
      }else if(od_days>=181 ){
          s="7";
      }else{
          s="N";
      }
      return s;
   }
    /**
     * 判断本期是否逾期
     * @param due_date_k
     * @param list
     * @param n
     * @return
     */
   private  static  int check_overdue_days(String due_date_k,List<String> list,int n,String ajxt_get_date){
        int is_overdue=0;
        String arr[];
        int od_days=0;
        if(due_date_k.compareTo(ajxt_get_date)>=0){
            due_date_k = ajxt_get_date;
        }
        for(int i=0;i<=n;i++){
            arr =list.get(i).split(",");
            if(arr[1].compareTo(due_date_k)>0){
                od_days= DateUtils.date_diff(due_date_k,arr[0]);
                break;
            }

        }
        if(od_days<3){
            od_days=0;
        }
        if(od_days>0){
            od_days=od_days+1;
        }
       return od_days;
   }

    /**
     * 月份加1，并且获取月末最后一天
     * @param date_str
     * @return
     */
    public static String next_month_lasyday(String date_str){
        Calendar c1 = Calendar.getInstance();;
        c1.set(Integer.parseInt(date_str.substring(0,4)), Integer.parseInt(date_str.substring(5,7))+1, 1);
        c1.add(Calendar.DATE, -1);

        return  ymd_format.format(c1.getTime());
    }
   private String format_loan_24month_status(String loan_24month_status){
       if(loan_24month_status.length()>24){
           loan_24month_status= loan_24month_status.substring(loan_24month_status.length()-24,loan_24month_status.length());
       }else if(loan_24month_status.length()==24){

       }else{
           StringBuilder sb = new StringBuilder();
           for(int i=0;i<24-loan_24month_status.length();i++){
               sb.append("/");
           }
           loan_24month_status=sb.append(loan_24month_status).toString();
       }
       return loan_24month_status;
   }
    public static void main(String[] args) {
        AJXTLoan24MonthsStateUDF dfu = new AJXTLoan24MonthsStateUDF();
        List<String> list = new ArrayList<String>();
        list.add("2020-10-18,2020-10-15");
        list.add("2020-11-18,2021-06-18");
        list.add("2020-12-18,2021-07-18");
        list.add("2021-01-18,2199-12-31");
//        list.add("2021-02-18,2021-02-06");
//        list.add("2021-03-18,2021-02-20");
//        list.add("2021-04-18,2021-03-23");
//        list.add("2021-05-18,2199-12-31");
//        list.add("2021-06-18,2199-12-31");
//        list.add("2021-07-18,2199-12-31");
//        list.add("2021-08-18,2199-12-31");
//        list.add("2021-09-18,2199-12-31");


        System.out.println(dfu.evaluate("2020-09-18",list,"2021-07-31","2021-08-02"));
        System.out.println(next_month_lasyday("2021-12-10"));
        System.out.println(DateUtils.date_diff("2022-05-11","2022-05-08"));
    }
}
